package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.ReportService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 数据报告控制器
 */
@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "数据报告展示接口")
public class ReportController {


    @Autowired
    private MemberService memberService;


    @Autowired
    private OrderService orderService;

    /**
     * 获取前12个月，每月会员累计增长总数量
     * @return
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){

        List<String> months = new ArrayList<>();
        List<Integer> memeberCount = null;
        //获取当前日期
        LocalDate localDate = LocalDate.now();

        //构造months
        for (int i = 11; i >=0 ; i--) {  //往前数12个月
            LocalDate newDate = localDate.minusMonths(i);
            String month = newDate.format(DateTimeFormatter.ofPattern("yyyy.MM"));
            log.info(month);
            months.add(month);
        }

        //构造memberCount  查询过去十二个月 每个月的会员数量
        memeberCount = memberService.countByMonth(months);

        Map<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memeberCount);
        return  new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

    }


    /**
     * 套餐占比统计
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {

        // 查询统计数据
        List<Map<String, Object>> setMealCount = orderService.countBySetMeal();
        //构造 setMealNames
        List<String> setMealNames = new ArrayList<>();
        for (Map<String, Object> elem : setMealCount) {
            setMealNames.add(String.valueOf(elem.get("name")));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames", setMealNames);
        map.put("setmealCount", setMealCount);
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);

    }

    @Autowired
    private ReportService reportService;

    /**
     * 获取运营统计数据
     * @return
     */
    @RequestMapping("/getBusinessReportData")
    public Result  getBusinessReportData(){

        Map<String,Object> map = reportService.getBussinessReport();
        log.debug("[运营报告]rpc data:{}",map);
        return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,map);

    }



    /**
     * 导出运营数据报表
     */
    @GetMapping("exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response) {
        log.info("[导出运营数据报表]开始");

        reportService.exportBusinessReport(response);
    }
}
